<template>
	<view class="auth-container" v-if="!userStore.isLogin">
		<view class="auth-header">
			<image class="logo" src="/static/logo.jpg" mode="aspectFit"></image>
			<view class="title">智慧药箱</view>
			<view class="subtitle">授权登录后体验更多功能</view>
		</view>
		
		<view class="auth-content">
			<view class="auth-tips">
				<view class="tip-item">
					<uni-icons type="checkmarkempty" size="20" color="#2ac2d1"></uni-icons>
					<text class="tip-text">查看药品详情</text>
				</view>
				<view class="tip-item">
					<uni-icons type="checkmarkempty" size="20" color="#2ac2d1"></uni-icons>
					<text class="tip-text">创建药品组合</text>
				</view>
				<view class="tip-item">
					<uni-icons type="checkmarkempty" size="20" color="#2ac2d1"></uni-icons>
					<text class="tip-text">设置用药提醒</text>
				</view>
			</view>
		</view>
		
		<view class="auth-footer">
			<button class="auth-btn" type="primary" @click="handleLogin">微信一键登录</button>
			<view class="privacy-policy">
				登录即代表您已同意
				<text class="link" @click="navigateToPrivacy">《用户协议和隐私政策》</text>
			</view>
		</view>
	</view>
	<view class="auth-container" v-else>
		<view class="user-info">
			<!-- 头像区域 -->
			<view class="avatar-section" @click="chooseAvatar">
				<image class="avatar" :src="userStore.userInfo.avatarUrl || '/static/default-avatar.png'" mode="aspectFill"></image>
				<view class="edit-icon">
					<uni-icons type="camera-filled" size="20" color="#fff"></uni-icons>
				</view>
			</view>
			
			<!-- 用户信息列表 -->
			<view class="info-list">
				<!-- 昵称 -->
				<view class="info-item">
					<view class="item-label">
						<text>昵称</text>
					</view>
					<view class="item-content">
						<input 
							type="text"
							v-model="userInfo.nickName"
							placeholder="设置昵称"
							@blur="updateUserInfo('nickName')"
						/>
						<uni-icons type="right" size="16" color="#999"></uni-icons>
					</view>
				</view>
				
				<!-- 手机号 -->
				<view class="info-item">
					<view class="item-label">
						<text>手机号</text>
					</view>
					<view class="item-content">
						<text v-if="userInfo.phone">{{formatPhone(userInfo.phone)}}</text>
						<text v-else class="placeholder">未绑定</text>
						<button 
							v-if="!userInfo.phone"
							class="bind-phone-btn"
							@click="bindPhoneNumber"
						>绑定手机号</button>
						<uni-icons type="right" size="16" color="#999"></uni-icons>
					</view>
				</view>
			</view>
			
			<!-- 退出登录按钮 -->
			<view class="logout-section">
				<button class="logout-btn" @click="handleLogout">退出登录</button>
			</view>
		</view>
	</view>
</template>

<script>
	import navigator from '@/utils/navigator'
	import { useUserStore } from '../../store/user'
	export default {
		data() {
			return {
				loading: false,
				userInfo: {
					nickName: '',
					phone: '',
					avatarUrl: ''
				}
			}
		},
		setup() {
			const userStore = useUserStore()
			return {
				userStore
			}
		},
		onShow() {
			// 初始化用户信息
			if (this.userStore.isLogin) {
				this.userInfo = { ...this.userStore.userInfo }
			}
		},
		methods: {
			async handleLogin() {
				if (this.loading) return
				this.loading = true
				
				try {
					uni.login({
						provider: 'weixin',
						success: (res) => {
							console.log(res.code);
							
							this.userStore.fetchUserInfo(res.code)
						},
						fail: (err) => {
							console.log(err)
						}
					})

					uni.showToast({
						title: '登录成功',
						icon: 'success'
					})
					
				} catch (e) {
					console.error('登录失败:', e)
					uni.showToast({
						title: '登录失败',
						icon: 'none'
					})
				} finally {
					this.loading = false
				}
			},
			// 选择头像
			chooseAvatar() {
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success: async (res) => {
						const tempFilePath = res.tempFilePaths[0]
						this.userInfo.avatarUrl = tempFilePath
						await this.updateUserInfo('avatarUrl')
					}
				})
			},
			
			// 更新用户信息
			async updateUserInfo(field) {
				try {
					await this.userStore.updateUserInfo({
						[field]: this.userInfo[field]
					})
					uni.showToast({
						title: '更新成功',
						icon: 'success'
					})
				} catch (error) {
					uni.showToast({
						title: '更新失败',
						icon: 'none'
					})
				}
			},
			
			// 绑定手机号
			bindPhoneNumber() {
				uni.getPhoneNumber({
					success: async (res) => {
						try {
							// 这里需要调用后端接口解密手机号
							const phone = await this.userStore.decryptPhone(res.code)
							this.userInfo.phone = phone
							await this.updateUserInfo('phone')
						} catch (error) {
							uni.showToast({
								title: '绑定失败',
								icon: 'none'
							})
						}
					},
					fail: () => {
						uni.showToast({
							title: '用户取消授权',
							icon: 'none'
						})
					}
				})
			},
			
			// 格式化手机号
			formatPhone(phone) {
				return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
			},
			
			// 退出登录
			handleLogout() {
				uni.showModal({
					title: '提示',
					content: '确定要退出登录吗？',
					success: (res) => {
						if (res.confirm) {
              this.userStore.logout()
							uni.showToast({
								title: '已退出登录',
								icon: 'success'
							})
						}
					}
				})
			},
		},
	}
</script>

<style lang="scss">
.auth-container {
	min-height: 100vh;
	height: 100vh;
	padding: 40rpx;
	display: flex;
	flex-direction: column;
	background-color: #ffffff;
	box-sizing: border-box;
	
	.auth-header {
		flex: none;
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 40rpx 0;
		
		.logo {
			width: 140rpx;
			height: 140rpx;
			margin-bottom: 20rpx;
		}
		
		.title {
			font-size: 36rpx;
			font-weight: bold;
			color: #333;
			margin-bottom: 16rpx;
		}
		
		.subtitle {
			font-size: 28rpx;
			color: #666;
		}
	}
	
	.auth-content {
		flex: 1;
		display: flex;
		align-items: center;
		
		.auth-tips {
			width: 100%;
			margin: 0;
			
			.tip-item {
				display: flex;
				align-items: center;
				margin-bottom: 30rpx;
				
				&:last-child {
					margin-bottom: 0;
				}
				
				.tip-text {
					font-size: 28rpx;
					color: #333;
					margin-left: 20rpx;
				}
			}
		}
	}
	
	.auth-footer {
		flex: none;
		padding: 40rpx 0;
		
		.auth-btn {
			background-color: #2ac2d1;
			border: none;
			
			&::after {
				border: none;
			}
			
			&.button-hover {
				background-color: #24a8b5;
			}
		}
		
		.privacy-policy {
			font-size: 24rpx;
			color: #999;
			text-align: center;
			margin-top: 20rpx;
			
			.link {
				color: #2ac2d1;
			}
		}
	}
	
	.user-info {
		padding: 20rpx;
		
		.avatar-section {
			position: relative;
			width: 160rpx;
			height: 160rpx;
			margin: 40rpx auto;
			
			.avatar {
				width: 100%;
				height: 100%;
				border-radius: 50%;
				background-color: #f5f5f5;
			}
			
			.edit-icon {
				position: absolute;
				right: 0;
				bottom: 0;
				width: 48rpx;
				height: 48rpx;
				background-color: rgba(0, 0, 0, 0.6);
				border-radius: 50%;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
		
		.info-list {
			margin-top: 40rpx;
			background-color: #fff;
			border-radius: 12rpx;
			
			.info-item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 30rpx;
				border-bottom: 2rpx solid #f5f5f5;
				
				&:last-child {
					border-bottom: none;
				}
				
				.item-label {
					color: #333;
					font-size: 28rpx;
				}
				
				.item-content {
					flex: 1;
					display: flex;
					justify-content: flex-end;
					align-items: center;
					
					input {
						text-align: right;
						margin-right: 20rpx;
					}
					
					.placeholder {
						color: #999;
						margin-right: 20rpx;
					}
					
					.bind-phone-btn {
						margin: 0 20rpx;
						padding: 0 20rpx;
						height: 56rpx;
						line-height: 56rpx;
						font-size: 24rpx;
						color: #2ac2d1;
						background: rgba(42, 194, 209, 0.1);
						border-radius: 28rpx;
						
						&::after {
							border: none;
						}
					}
				}
			}
		}
		
		.logout-section {
			margin-top: 60rpx;
			padding: 0 40rpx;
			
			.logout-btn {
				width: 100%;
				height: 88rpx;
				line-height: 88rpx;
				text-align: center;
				background-color: #fff;
				color: #ff4d4f;
				border-radius: 44rpx;
				font-size: 32rpx;
				
				&::after {
					border: none;
				}
				
				&.button-hover {
					background-color: #f5f5f5;
				}
			}
		}
	}
}
</style> 